﻿using QuanLyThuVien.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QuanLyThuVien.Supporter;
using System.Data.SqlClient;
using System.Data;

namespace QuanLyThuVien.DAL
{
    //----------------------------------------------------------------------
    //Desc: class kết nối với bảng PHIEUTHU
    //----------------------------------------------------------------------
    public class DALPhieuThu
    {
        //----------------------------------------------------------------------
        //Desc: constructor mặc định
        //----------------------------------------------------------------------
        public DALPhieuThu()
        { }

        //----------------------------------------------------------------------
        //Desc: chuyển dòng dữ liệu thành PHIEUTHU
        //----------------------------------------------------------------------
        public DTOPhieuThu ConvertToPhieuThu(DataRow row)
        {
            DTOPhieuThu phieuthu = new DTOPhieuThu();
            phieuthu.MaPhieuThu = row["MaPhieuThu"].ToString();
            try
            {
                phieuthu.NgayLap = DateTime.Parse(row["NgayLap"].ToString());
            }
            catch{ }
            phieuthu.DocGia.MaDG = row["MaDG"].ToString();
            try
            {
                phieuthu.SoTienThu = Double.Parse(row["TienThu"].ToString());
            }
            catch { }
            try
            {
                phieuthu.ConLai = Double.Parse(row["ConLai"].ToString());
            }
            catch { }

            return phieuthu;
        }

        //----------------------------------------------------------------------
        //Desc: lấy danh sách phiếu thu
        //----------------------------------------------------------------------
        public DataTable LayDanhSachPhieuThu()
        {
            if (DatabaseManager.DbConnection == null)
                return null;
            string spName = "spPhieuThu_LayDanhSachPhieuThu";
            return DatabaseManager.DbConnection.ExecuteStoredProcedure(spName);
        }

        //----------------------------------------------------------------------
        //Desc: lấy phiếu thu
        //----------------------------------------------------------------------
        public DTOPhieuThu LayPhieuThu(string maphieuthu)
        {
            if (DatabaseManager.DbConnection == null)
                return null;
            string spName = "spPhieuThu_LayPhieuThu";
            SqlParameter sqlprMaPhieuThu = new SqlParameter("@MaPhieuThu", SqlDbType.VarChar, 15)
            { Value = maphieuthu };
            DataTable data = DatabaseManager.DbConnection.ExecuteStoredProcedure(spName, sqlprMaPhieuThu);
            if (data == null || data.Rows.Count == 0)
                return null;
            return ConvertToPhieuThu(data.Rows[0]);
        }

        //----------------------------------------------------------------------
        //Desc: Lấy danh sách phiếu thu của độc giả
        //----------------------------------------------------------------------
        public DataTable LayPhieuThuDocgia(string madocgia)
        {
            if (DatabaseManager.DbConnection == null)
                return null;
            string spName = "spPhieuThu_LayPhieuThuDocGia";
            SqlParameter sqlprMaDG = new SqlParameter("@MaDG", SqlDbType.VarChar, 15) { Value = madocgia };
            return DatabaseManager.DbConnection.ExecuteStoredProcedure(spName, sqlprMaDG);
        }

        //----------------------------------------------------------------------
        //Desc: đếm số phiếu thu
        //----------------------------------------------------------------------
        public int DemSoPhieuThu()
        {
            if (DatabaseManager.DbConnection == null)
                return -1;
            string spName = "spPhieuThu_DemPhieuThu";
             object obj = DatabaseManager.DbConnection.ExecuteStoredProcedureScalar(spName);

            if (obj == null)
                return -1;

            int dem;
            try
            {
                dem = (int)obj;
            }
            catch
            {
                return -1;
            }
            return dem;
        }

        //----------------------------------------------------------------------
        //Desc: đếm mã phiếu thu
        //----------------------------------------------------------------------
        public int DemMaPhieuThu(string maphieuthu)
        {
            if (DatabaseManager.DbConnection == null)
                return -1;
            string spName = "spPhieuThu_DemMaPhieuThu";
            SqlParameter sqlprMaPhieuThu = new SqlParameter("@MaPhieuThu", SqlDbType.VarChar, 15) { Value = maphieuthu };
            object obj = DatabaseManager.DbConnection.ExecuteStoredProcedureScalar(spName, sqlprMaPhieuThu);
            if (obj == null)
                return -1;

            int dem;
            try
            {
                dem = (int)obj;
            }
            catch
            {
                return -1;
            }
            return dem;
        }

        //----------------------------------------------------------------------
        //Desc: cập nhật phiếu thu
        //----------------------------------------------------------------------
        public bool CapNhat(DTOPhieuThu phieuthu)
        {
            if (DatabaseManager.DbConnection == null)
                return false;
            string spName = "spPhieuThu_CapNhatPhieuThu";
            SqlParameter sqlprThaoTac = new SqlParameter("@ThaoTac", SqlDbType.Int) { Value = 1 };
            SqlParameter sqlprMaPhieuThu = new SqlParameter("@MaPhieuThu", SqlDbType.VarChar, 15) { Value = phieuthu.MaPhieuThu };
            SqlParameter sqlprNgayLap = new SqlParameter("@NgayLap", SqlDbType.Date) { Value = phieuthu.NgayLap };
            SqlParameter sqlprMaDG = new SqlParameter("@MaDG", SqlDbType.VarChar, 15) { Value = phieuthu.DocGia.MaDG };
            SqlParameter sqlprTienThu = new SqlParameter("@TienThu", SqlDbType.Money) { Value = phieuthu.SoTienThu };
            SqlParameter sqlprConLai = new SqlParameter("@ConLai", SqlDbType.Money) { Value = phieuthu.ConLai };
            return DatabaseManager.DbConnection.ExecuteStoredProcedureNonQuery(spName, sqlprThaoTac, sqlprMaPhieuThu, sqlprNgayLap,
                sqlprMaDG, sqlprTienThu, sqlprConLai);
        }

    }
}
